﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Yahoo.Yui.Compressor;

namespace YUICustomTool
{
    [Guid("E9A6345A-1DE6-4023-A092-F755581EE676")]
    [ComVisible(true)]
    public class YUICSSAutoCompressor : Microsoft.VisualStudio.TextTemplating.VSHost.BaseCodeGeneratorWithSite
    {
        private string inputFile;
        public override string GetDefaultExtension()
        {
            if (inputFile.EndsWith(".css"))
                return ".min.css";
            else if (inputFile.EndsWith(".js"))
                return ".min.js";
            else
            {
                CreateExceptionMessage(new NotImplementedException("This file type has not been implemented. Accepted file types are .js and .css."));
                return ".min";
            }
        }

        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            inputFile = inputFileName;
            if (inputFile.EndsWith(".css"))
                return Encoding.UTF8.GetBytes(Yahoo.Yui.Compressor.CssCompressor.Compress(inputFileContent));
            else if (inputFile.EndsWith(".js"))
                return Encoding.UTF8.GetBytes(Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(inputFileContent));
            else
                return null;
        }
    }
}
